--------------- Common Configs --------------
local keymap = lvim.builtin.which_key.mappings
local vkeymap = lvim.builtin.which_key.vmappings
vim.opt.relativenumber = true

-- my plugins
require('myplugs')

local dap_breakpoint_color = {
  breakpoint = {
    ctermbg = 0,
    fg = '#993939',
    bg = '#31353f',
  },
  logpoing = {
    ctermbg = 0,
    fg = '#61afef',
    bg = '#31353f',
  },
  stopped = {
    ctermbg = 0,
    fg = '#98c379',
    bg = '#31353f'
  },
}

vim.api.nvim_set_hl(0, 'DapBreakpoint', dap_breakpoint_color.breakpoint)
vim.api.nvim_set_hl(0, 'DapLogPoint', dap_breakpoint_color.logpoing)
vim.api.nvim_set_hl(0, 'DapStopped', dap_breakpoint_color.stopped)

lvim.keys.normal_mode['<F5>'] = ":lua require'dap'.continue()<CR>"
lvim.keys.normal_mode['<F6>'] = ":lua require'dap'.step_over()<CR>"
lvim.keys.normal_mode['<F7>'] = ":lua require'dap'.step_into()<CR>"
lvim.keys.normal_mode['<F8>'] = ":lua require'dap'.step_out()<CR>"
lvim.keys.normal_mode['<F9>'] = ":lua require'dap'.toggle_breakpoint()<CR>"
lvim.keys.normal_mode['<F10>'] = ":lua require'dap'.terminate()<CR>"
lvim.keys.normal_mode['<F12>'] = ":lua require'dap'.run_to_cursor()<CR>"

lvim.format_on_save.enabled = true
lvim.format_on_save.pattern = { "*.lua", "*.py", "*.c", "*.cpp", "*.cc", "*.hpp" }

vim.cmd("nnoremap gpd <cmd>lua require('goto-preview').goto_preview_definition()<CR>")
vim.cmd("nnoremap gpi <cmd>lua require('goto-preview').goto_preview_implementation()<CR>")
vim.cmd("nnoremap gP <cmd>lua require('goto-preview').close_all_win()<CR>")
lvim.builtin.which_key.mappings["S"] = {
  name = "Session",
  c = { "<cmd>lua require('persistence').load()<cr>", "Restore last session for current dir" },
  l = { "<cmd>lua require('persistence').load({ last = true })<cr>", "Restore last session" },
  Q = { "<cmd>lua require('persistence').stop()<cr>", "Quit without saving session" },
}

-- common python dap configs
-- *** attention ***: if you want to debug python in your activated conda env, You Need Install debugpy in Conda Envs, for example:
-- conda install debugpy
-- dap.adapters.python = function(cb, config)
local dap = require('dap')
dap.adapters.python = {
  type = 'executable',
  command = 'python',
  args = { '-m', 'debugpy.adapter' },
  options = {
    source_filetype = 'python',
  },
}

dap.configurations.python = {
  {
    type = 'python', -- the type here established the link to the adapter definition: `dap.adapters.python`
    request = 'launch',
    name = "Launch file",
    program = "${file}", -- This configuration will launch the current file if used.
    pythonPath = 'python',
  },
}

-- common c/c++/rust dap configs (codelldb)
dap.adapters.codelldb = {
  type = 'server',
  host = '127.0.0.1',
  port = "${port}",
  executable = {
    -- 需要在Mason中安装DAP:codelldb，以下路径是codelldb的安装路径
    command = 'codelldb',
    args = { "--port", "${port}" },
  }
}

dap.configurations.cpp = {
  {
    name = "Launch file",
    type = "codelldb",
    request = "launch",
    program = function()
      return vim.fn.input('Path to executable: ', vim.fn.getcwd() .. '/', 'file')
    end,
    cwd = '${workspaceFolder}',
    stopOnEntry = false,
  },
}

dap.configurations.c = dap.configurations.cpp
dap.configurations.rust = dap.configurations.cpp

-------------- Windows Dap Config ---------------
-- ****** If you are using lunarvim on WINDOWS, UnComment these configs
-- windows python dap configs, using pythonw instead of python
dap.configurations.python = {
  {
    type = 'python', -- the type here established the link to the adapter definition: `dap.adapters.python`
    request = 'launch',
    name = "Launch file",
    program = "${file}", -- This configuration will launch the current file if used.
    pythonPath = 'pythonw',
  },
}

-- local cmp = require "cmp"
-- cmp.setup({
--   sources = {
--     { name = "codeium" },
--   },
-- })
lvim.builtin.treesitter.rainbow.enable = true
keymap["os"] = { name = "+Spectre" }
keymap["oss"] = { "<cmd>lua require('spectre').open()<cr>", "Spectre Open" }
vkeymap["osw"] = { "<cmd>lua require('spectre').open_visual({select_word=true})<cr>", "Spectre in Visual Word" }
vkeymap["osv"] = { "<esc><cmd>lua require('spectre').open_visual()<CR>", "Spectre in Visual" }
keymap["osf"] = { "viw<cmd>lua require('spectre').open_file_search()<CR>", "Spectre in File" }

-- search.replace.nvim config BEGIN
keymap["r"] = {
  name = "SearchReplaceSingleBuffer",
  s = { "<CMD>SearchReplaceSingleBufferSelections<CR>", "SearchReplaceSingleBuffer [s]elction list" },
  o = { "<CMD>SearchReplaceSingleBufferOpen<CR>", "[o]pen" },
  w = { "<CMD>SearchReplaceSingleBufferCWord<CR>", "[w]ord" },
  W = { "<CMD>SearchReplaceSingleBufferCWORD<CR>", "[W]ORD" },
  e = { "<CMD>SearchReplaceSingleBufferCExpr<CR>", "[e]xpr" },
  f = { "<CMD>SearchReplaceSingleBufferCFile<CR>", "[f]ile" },
  b = {
    name = "SearchReplaceMultiBuffer",
    s = { "<CMD>SearchReplaceMultiBufferSelections<CR>", "SearchReplaceMultiBuffer [s]elction list" },
    o = { "<CMD>SearchReplaceMultiBufferOpen<CR>", "[o]pen" },
    w = { "<CMD>SearchReplaceMultiBufferCWord<CR>", "[w]ord" },
    W = { "<CMD>SearchReplaceMultiBufferCWORD<CR>", "[W]ORD" },
    e = { "<CMD>SearchReplaceMultiBufferCExpr<CR>", "[e]xpr" },
    f = { "<CMD>SearchReplaceMultiBufferCFile<CR>", "[f]ile" },
  },
}
lvim.keys.visual_block_mode["<C-r>"] = [[<CMD>SearchReplaceSingleBufferVisualSelection<CR>]]
lvim.keys.visual_block_mode["<C-s>"] = [[<CMD>SearchReplaceWithinVisualSelection<CR>]]
lvim.keys.visual_block_mode["<C-b>"] = [[<CMD>SearchReplaceWithinVisualSelectionCWord<CR>]]
vim.o.inccommand = "split"
-- search.replace.nvim config END
